﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace ConsumoDeAPI
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

       

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += (s, E) =>
            {
                ConsumoDeAPI.Model.ServiceModel sm = new Model.ServiceModel(new Uri("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b604df2b2b5b46f85215b57bfd1e5d83&text=Barquisimeto&sort=relevance&content_type=1&per_page=10&page=1&format=rest&auth_token=72157634211850255-4bee7f453fbc5bd1&api_sig=e97ccb7c58edcf75566449d2279357ea"));
                sm.ServiceResponseReady += new EventHandler<Model.ResponseEventArg>(sm_ServiceResponseReady);
                sm.Request();
            
            };
        }

        void sm_ServiceResponseReady(object sender, Model.ResponseEventArg e)
        {
            string r = (e).Result;
            XDocument doc = XDocument.Parse(r);
            var rr = (from ph in doc.Descendants("photo") select new { title = ph.Attribute("title").Value,
                                                                        id = ph.Attribute("id").Value,
                                                                        farm = ph.Attribute("farm").Value,
                                                                        server = ph.Attribute("server").Value,
                                                                        secret = ph.Attribute("secret").Value
            }).ToList();
            this.Dispatcher.Invoke(new System.Action(() =>
            {
                foreach (var item in rr) {
                    resultText.Text += string.Format("http://farm{0}.staticflickr.com/{1}/{2}_{3}_b.jpg", item.farm, item.server, item.id, item.secret) + " \n";
                }
            }), new object[] { });
        }
    }
}
